<?php
namespace app\index\controller;

use think\Request;
use think\Controller;
use think\Session;

//零售客户这边的 小店
class Shop extends xiaodian{
	
	//零售客户看到的小店首页
	public function index(){
		$pm = [
			'agentid' => SHOP_ID
		];
		$response = \ServerHelper::post('shop_index',$pm);
		$data = $this->J($response);
		if(empty($data['flag'])){
			$this->error(isset($data['msg']) ? $data['msg'] : '数据错误');
		}
		$data['data']['showback'] = Session::has('agent_id');
		//return view('index',$data);
		
		$link = '[{"id":93979,"link":"/shop/detail/id/53.do","title":"","class":"","z_index":105,"left":7.01,"top":0.13,"width":26.05,"height":4.88,"width_percent":26.05,"height_percent":4.88},{"id":69594,"link":"/shop/detail/id/50.do","title":"","class":"","z_index":106,"left":34.21,"top":1.76,"width":59,"height":3.32,"width_percent":59,"height_percent":3.32},{"id":37695,"link":"/shop/detail/id/42.do","title":"","class":"","z_index":108,"left":23.11,"top":14.04,"width":24.9,"height":5.46,"width_percent":24.9,"height_percent":5.46},{"id":32164,"link":"/shop/detail/id/48.do","title":"","class":"","z_index":109,"left":50.06,"top":13.97,"width":25.03,"height":5.59,"width_percent":25.03,"height_percent":5.59},{"id":83838,"link":"/shop/detail/id/45.do","title":"","class":"","z_index":110,"left":10.98,"top":19.68,"width":24.77,"height":5.49,"width_percent":24.77,"height_percent":5.49},{"id":61321,"link":"/shop/detail/id/47.do","title":"","class":"","z_index":112,"left":63.46,"top":19.64,"width":25.41,"height":5.59,"width_percent":25.41,"height_percent":5.59},{"id":34264,"link":"/shop/detail/id/56.do","title":"","class":"","z_index":113,"left":6.64,"top":27.6,"width":86.07,"height":8.43,"width_percent":86.07,"height_percent":8.43},{"id":87925,"link":"/shop/detail/id/40659.do","title":"","class":"","z_index":116,"left":11.62,"top":48.1,"width":24.9,"height":5.49,"width_percent":24.9,"height_percent":5.49},{"id":48247,"link":"/shop/detail/id/40661.do","title":"","class":"","z_index":117,"left":37.8,"top":48.08,"width":25.03,"height":5.59,"width_percent":25.03,"height_percent":5.59},{"id":27246,"link":"/shop/detail/id/40660.do","title":"","class":"","z_index":118,"left":63.85,"top":48.13,"width":25.41,"height":5.57,"width_percent":25.41,"height_percent":5.57},{"id":48928,"link":"","title":"","class":"","z_index":119,"left":8.17,"top":56.44,"width":86.07,"height":4.13,"width_percent":86.07,"height_percent":4.13},{"id":24803,"link":"/shop/detail/id/60.do","title":"精粹霜","class":"","z_index":120,"left":8.3,"top":60.86,"width":85.95,"height":4.05,"width_percent":85.95,"height_percent":4.05},{"id":54412,"link":"/shop/detail/id/58.do","title":"","class":"","z_index":121,"left":8.04,"top":65.05,"width":42.01,"height":7.01,"width_percent":42.01,"height_percent":7.01},{"id":43928,"link":"/shop/detail/id/59.do","title":"","class":"","z_index":122,"left":51.46,"top":65.03,"width":42.4,"height":6.8,"width_percent":42.4,"height_percent":6.8},{"id":81682,"link":"/shop/detail/id/34620.do","title":"","class":"","z_index":123,"left":7.15,"top":73.98,"width":42.4,"height":7.02,"width_percent":42.4,"height_percent":7.02},{"id":49543,"link":"/shop/detail/id/34621.do","title":"","class":"","z_index":124,"left":50.57,"top":73.93,"width":42.52,"height":7.06,"width_percent":42.52,"height_percent":7.06},{"id":89462,"link":"/shop/detail/id/33967.do","title":"","class":"","z_index":125,"left":12.89,"top":83.76,"width":20.94,"height":4.88,"width_percent":20.94,"height_percent":4.88},{"id":29433,"link":"/shop/detail/id/33965.do","title":"","class":"","z_index":126,"left":37.42,"top":83.76,"width":24.01,"height":4.9,"width_percent":24.01,"height_percent":4.9},{"id":97870,"link":"/shop/detail/id/43.do","title":"","class":"","z_index":127,"left":65.38,"top":83.76,"width":25.28,"height":4.82,"width_percent":25.28,"height_percent":4.82},{"id":44461,"link":"/shop/detail/id/33966.do","title":"黄金乳","class":"","z_index":128,"left":12.38,"top":88.82,"width":22.34,"height":4.84,"width_percent":22.34,"height_percent":4.84},{"id":48916,"link":"/shop/detail/id/44.do","title":"","class":"","z_index":129,"left":36.65,"top":88.87,"width":25.54,"height":4.69,"width_percent":25.54,"height_percent":4.69},{"id":49858,"link":"/shop/detail/id/33964.do","title":"","class":"","z_index":130,"left":64.24,"top":88.85,"width":27.58,"height":4.66,"width_percent":27.58,"height_percent":4.66},{"id":60420,"link":"/shop/detail/id/39527.do","title":"眼膜","class":"","z_index":131,"left":12.5,"top":94.06,"width":79.43,"height":3.86,"width_percent":79.43,"height_percent":3.86},{"id":33741,"link":"","title":"","class":"shop-item-sctop","z_index":132,"left":0,"top":98.1,"width":99.1,"height":1.66,"width_percent":99.1,"height_percent":1.66},{"id":42810,"link":"/shop/detail/id/46.do","title":"","class":"","z_index":140,"left":37.54,"top":19.63,"width":25.03,"height":5.71,"width_percent":25.03,"height_percent":5.71},{"id":91546,"link":"/shop/detail/id/40.do","title":"","class":"","z_index":147,"left":6.76,"top":38.3,"width":42.65,"height":7.02,"width_percent":42.65,"height_percent":7.02},{"id":63485,"link":"/shop/detail/id/41.do","title":"","class":"","z_index":151,"left":50.7,"top":38.32,"width":42.78,"height":7.07,"width_percent":42.78,"height_percent":7.07}]';
		$data['data']['link'] = json_decode($link,true);
		return view('index_new',$data);
	}
	
	//取消代理进入小店时的跨店提示
	public function cancelkdts(){
		Session::set('cancelkdts','1');
		ajax('SUCCESS',true);
	}
	
	//小店产品列表
	public function products(){
		$param = [
			'agentid' => SHOP_ID,
			'customerid' => CUST_ID
		];
		$response = \ServerHelper::post('shop_productlist',$param);
		$data = $this->J($response);
		if(Session::get('cancelkdts') == '1'){
			$data['data']['tips'] = '';
		}
		return view('',['data'=>$data['data']]);
	}
	
	//小店商品详情页
	public function detail(){
		$id = Request::instance()->param('id');
		if(!\RegexHelper::is_id($id)){
			abort(404,'页面不存在');
		}
		$p = [
			'agentid'=>SHOP_ID,
			'customerid'=>CUST_ID,
			'productid'=>$id
		];
		$detail = \ServerHelper::post('shop_getproductdetail',$p);
		$data = $this->J($detail);
		$v = 'detail';
		$d = $data['data'];
		switch($data['flag']){
			case 0 :
				$v = 'detail_404';
				break;
			case 3 :
				$v = 'detail_xiajia';
				$d = $data['msg'];
				break;
			default :
				break;
		}
		return view($v,['data'=>$d]);
	}
	
	#region 购物车
	//购物车首页
    public function cart(){
		$result = \ServerHelper::post('shop_cart_index',[
			'agentid'=>SHOP_ID,
			'customerid'=>CUST_ID,
		]);
		$data = $this->J($result);
		return view('',[
			'data' => $data['data']
		]);
    }
	
	//加购物车
	public function cart_add(){
		
		$goods_str = Request::instance()->param('goods_id');
		$amount_str = Request::instance()->param('amount');
		if(!\RegexHelper::is_ids($goods_str) || !\RegexHelper::is_ids($amount_str)){
			ajax('参数错误',false,0);
		}
		
		$goods_arr = explode(',',$goods_str);
		$amount_arr = explode(',',$amount_str);
		$goods_arr = array_unique($goods_arr); //ID可以，数量不能去重
		
		if(count($goods_arr) != count($amount_arr)){
			ajax('参数错误',false,0);
		}
		
		$products = [];
		for($i=0;$i<count($goods_arr);$i++){
			$products[] = [
				'productid' => "{$goods_arr[$i]}",
				'buynum' => (int)$amount_arr[$i]
			];
		}
		
		$postData = [
			'agentid'=>SHOP_ID,
			'customerid'=>CUST_ID,
			'products'=> json_encode($products)
		];
		
		$result = \ServerHelper::post('shop_cart_add',$postData);
		
		$data = $this->J($result);
		
		if($data['flag']){
			ajax('SUCCESS',true,[
				'list'=>$data['data'],
				'total_price'=>$data['orderamount'],
				'total_amount'=>$data['producttotalnum'],
			]);
		}else{
			ajax($data['msg'],false,[
				'list'=>$data['data'],
				'total_price'=>$data['orderamount'],
				'total_amount'=>$data['producttotalnum'],
			]);
		}
	}
	#endregion
	
	#region 下单流程
	public function order_confirm(){
		
		//接收ID
		$goods_str = Request::instance()->param('goods_id','',REG_IDS);
		$amount_str = Request::instance()->param('amount','',REG_IDS);
		
		if(!\RegexHelper::is_ids($goods_str) || !\RegexHelper::is_ids($amount_str)){
			$this->error('请选择商品');
		}
		
		$goods_arr = explode(',',$goods_str);
		$amount_arr = explode(',',$amount_str);
		$goods_arr = array_unique($goods_arr); //ID可以，数量不能去重
		
		if(count($goods_arr) != count($amount_arr)){
			$this->error('参数错误');
		}
		
		$products = [];
		for($i=0;$i<count($goods_arr);$i++){
			$products[] = [
				'productid' => (int)$goods_arr[$i],
				'buynum' => (int)$amount_arr[$i]
			];
		}
		
		$postData = [
			'agentid'=>SHOP_ID,
			'customerid'=>CUST_ID,
			'products'=> json_encode($products)
		];
		
		//发送ID，得到商品
		$result = \ServerHelper::post('shop_orderconfirm',$postData);

		$data = $this->J($result);

		$express_list = []; //用于下拉
		$express_list_full = []; //用于切换后计算运费
		$express_default = ['id'=>'','value'=>'请选择','index'=>0];
		$_index = 0;
		
		//快递信息
		foreach($data['data']['express'] as $v){
			$express_list[] = ['id'=>$v['expressid'],'value'=>$v['expresscompany']];
			if($v['isdefault']){
				$express_default = [
					'id' => $v['expressid'],
					'value' => $v['expresscompany'],
					'expressprice' => $v['expressprice'],
					'index' => $_index //下拉绑定后显示哪个
				];
			}
			$express_list_full[$v['expressid']] = [
				'value' => $v['expresscompany'],
				'expressprice' => $v['expressprice']
			];
			$_index++;
		}
		
		$express = [
			'list' => json_encode($express_list,JSON_UNESCAPED_UNICODE),
			'listfull' => json_encode($express_list_full,JSON_UNESCAPED_UNICODE),
			'default' => $express_default
		];
		
		//得到前段需要的商品信息
		$products = [];
		foreach($data['data']['product'] as $v){
			$products[] = [
				'productid' => (int)$v['productid'],
				'buynum' => (int)$v['buynum'],
				'productprice' => (float)$v['productprice'],
				'productname' => $v['productname'],
			];
		}
		
		return view('',[
			'data' => $data['data'],
			'products' => json_encode($products,JSON_UNESCAPED_UNICODE),
			'express' => $express
		]);
	}
	
	//下单时修改地址，重新计算运费
	public function changeaddress(){
		$request = Request::instance();
		
		$postData = [
			'agentid' => SHOP_ID,
			'customerid' => CUST_ID,
			'tsid' => $request->param('tsid'),
			'products' => $request->param('products')
		];
		
		$response = \ServerHelper::post('shop_orderchangeaddress',$postData);
		$response = json_decode($response,true);
		if(empty($response['flag'])){
			ajax('运费获取失败');
		}
		
		$list = [];
		$listfull = [];
		foreach($response['data'] as $k=>$v){
			$list[] = [
				'id' => $v['expressid'],
				'value' => $v['expresscompany'],
			];
			$listfull[$v['expressid']] = [
				'id' => $v['expressid'],
				'value' => $v['expresscompany'],
				'expressprice' => $v['expressprice'],
			];
		}
		
		ajax('运费获取成功',true,[
			'lunzi' => $list,
			'list' => $listfull
		]);
	}
	
	//正式下单
	public function order_add(){
		$data = Request::instance()->param('data','','trim');

		$json = json_decode($data,true);
		if(empty($json) || empty($json['address'])){
			ajax('下单失败');
		}
		
		$orderparam = $json;
		$orderparam['agentid'] = SHOP_ID;
		$orderparam['customerid'] = CUST_ID;
		
		$postData = [
			'orderparam' => json_encode($orderparam)
		];
		
		$response = \ServerHelper::post('shop_orderadd',$postData);
		
		$data = $this->J($response);
		
		if($data['flag'] == 1){
			ajax($data['msg'],true,$data['data']['orderid']);
		}else{
			ajax($data['msg'],false);
		}
		
	}
	
	#endregion
	
	
	#region 支付
	
	//支付页面
	public function pay(){
		
		//D20200412214133E7C8D43D
		$orderid = Request::instance()->param('orderid');
		if($orderid == ''){
			$this->error('页面不存在');
		}
		
		$postData = [
			'agentid' => SHOP_ID,
			'customerid' => CUST_ID,
			'orderid' => $orderid
		];
		
		$responst = \ServerHelper::post('shop_pay_show',$postData);
		$data = $this->J($responst);
		
		if($data['flag'] == 0){
			$this->error('页面不存在');
		}

		$steps_new = null;
		foreach($data['data']['steps'] as $k=>$o){
			if($o['status'] == 0){
				$steps_new = $o;
				break;
			}
		}
		
		if(empty($steps_new)){
			//(货款orderamount 现金cashmount)都成功了，则跳转到列表去
			//jia yige 2bu dou zhifu chenggong zhanshi yemian
			
			$this->redirect('Shop/payresult',['orderid'=>$orderid]);
			
		}
		//去除已经支付了的步骤 且 仅保留当前要支付的步骤
		$data['data']['steps'] = $steps_new; 
		//附加订单ID
		$data['data']['orderid'] = $orderid;
		
		return view('',[
			'data' => $data['data']
		]);
	}
	
	//①余额支付
	public function blancepay(){
		
		//现金余额、货款余额
		$orderid = Request::instance()->param('orderid');
		$ordertype = Request::instance()->param('ordertype'); //订单类型(付哪一个步骤)
		$paytype = Request::instance()->param('paytype'); //支付类型
		
		if($orderid == ''){
			$this->error('订单号错误');
		}
		if($ordertype  == ''){
			$this->error('参数错误');
		}
		if($paytype == ''){
			$this->error('参数错误');
		}
		
		$request = [
			'agentid' => AGENT_ID,
			'orderid' => $orderid,
			'ordertype' => $ordertype,
			'paytype' => $paytype,
		];
		
		$response = \ServerHelper::post('balancepay',$request);
		$data = $this->J($response);
		
		if($data['flag']){
			$this->redirect('Shop/pay',['orderid'=>$orderid]);
		}else{
			$this->error($data['msg']);
		}

	}
	
	
	//②线下支付
	public function xxpay(){
		
		//现金余额、货款余额
		$orderid = Request::instance()->param('orderid');
		$ordertype = Request::instance()->param('ordertype'); //订单类型(付哪一个步骤)
		$paytype = Request::instance()->param('paytype'); //支付类型
		
		if($orderid == ''){
			$this->error('订单号错误');
		}
		if($ordertype  == ''){
			$this->error('参数错误');
		}
		if($paytype == ''){
			$this->error('参数错误');
		}
			
		if(Request::instance()->isPost()){
			
			$postdata = [
				'agentid' => SHOP_ID,
				'customerid' => CUST_ID,
				'orderid' => $orderid,
				'ordertype' => $ordertype,
				'paytype' => $paytype,
				'repaccounttype' => Request::instance()->param('repaccounttype'),
				'repaccount' => Request::instance()->param('repaccount'),
				//'reppicture' => '', //打款凭证
				'remark' => Request::instance()->param('remark'),
			];
			$reppicture = Request::instance()->param('reppicture');
			if($postdata['repaccounttype'] == '' || $postdata['repaccount'] == ''){
				$this->error('请选择收款人账户');
			}
			$arr = explode(',',$reppicture);
			if(count($arr) < 1 || substr($arr[0],0,4) != 'http'){
				$this->error('请上传打款凭证');
			}
			$postdata['reppicture'] = json_encode($arr);

			$response = \ServerHelper::post('shop_xxpay_do',$postdata);
			$response = $this->J($response);
			if($response['flag']){
				$this->redirect('Shop/pay',['orderid'=>$orderid]);
			}else{
				$this->error($response['msg']);
			}
			
		}else{
		
			$request = [
				'agentid' => SHOP_ID,
				'customerid' => CUST_ID,
				'orderid' => $orderid,
				'ordertype' => $ordertype,
				'paytype' => $paytype,
			];
			
			$response = \ServerHelper::post('shop_xxpay',$request);
			$response = $this->J($response);
			if($response['flag'] == 0){
				$this->error($response['msg']);
			}

			//选择数据
			$accountType = [];
			$accountTypeFull = [];
			if(isset($response['data']['repaccount']['wechat'])){
				$accountType[] = [
					'id'=>$response['data']['repaccount']['wechat']['accountid'],
					'value'=>$response['data']['repaccount']['wechat']['accountname'],
				];
				$accountTypeFull['wechat'] = [
					'id'=>$response['data']['repaccount']['wechat']['accountid'],
					'value'=>$response['data']['repaccount']['wechat']['accountname'],
				];
			}
			if(isset($response['data']['repaccount']['alipay'])){
				$accountType[] = [
					'id'=>$response['data']['repaccount']['alipay']['accountid'],
					'value'=>$response['data']['repaccount']['alipay']['accountname'],
				];
				$accountTypeFull['alipay'] = [
					'id'=>$response['data']['repaccount']['alipay']['accountid'],
					'value'=>$response['data']['repaccount']['alipay']['accountname'],
				];
			}
			if(isset($response['data']['repaccount']['bank'])){
				$accountType[] = [
					'id'=>$response['data']['repaccount']['bank']['accountid'],
					'value'=>$response['data']['repaccount']['bank']['accountname'],
				];
				$accountTypeFull['bank'] = [
					'id'=>$response['data']['repaccount']['bank']['accountid'],
					'value'=>$response['data']['repaccount']['bank']['accountname'],
				];
			}
			
			return view('',[
				'data' => $response['data'],
				'ordertype' => $ordertype,
				'paytype' => $paytype,
				'orderid' => $orderid,
				'accounttype' => json_encode($accountType,JSON_UNESCAPED_UNICODE),
				'accounttypefull' => json_encode($accountTypeFull,JSON_UNESCAPED_UNICODE)
			]);
		}
	}

	//③微信支付
	public function wepay(){
		
		//现金余额、货款余额
		$orderid = Request::instance()->param('orderid');
		$ordertype = Request::instance()->param('ordertype'); //订单类型(付哪一个步骤)
		$paytype = Request::instance()->param('paytype'); //支付类型
		
		if($orderid == ''){
			$this->error('订单号错误');
		}
		if($ordertype  == ''){
			$this->error('参数错误');
		}
		if($paytype == ''){
			$this->error('参数错误');
		}
		
		//拉取支付信息
		$request = [
			'agentid' => AGENT_ID,
			'orderid' => $orderid,
			'ordertype' => $ordertype,
			'paytype' => $paytype,
		];
		$payinfo = \ServerHelper::post('wepay_show',$request);
		$payinfo = $this->J($payinfo);
		if($payinfo['flag'] != 1){ $this->error('支付发起失败'); }

		__log('orderid：'.$payinfo['data']['orderid'].'，payid：'.$payinfo['data']['payid'],'tmp' );
    
		$param = [
			'body' => $payinfo['data']['products'],
			'orderid' => $payinfo['data']['orderid'],
			'pay_no' => $payinfo['data']['payid'],
			'price' => intval($payinfo['data']['amount']), //价格(单位：分)
			'notify_url' => Request::instance()->domain() . url('Wepay/notify_url'),
		];
		    
		$url = url('Wepay/index') . '?' . http_build_query($param);
    
		$this->redirect($url);
	}
	
	//支付结果
	public function payresult(){
		$orderid = Request::instance()->param('orderid');
		$response = \ServerHelper::post('shop_payresult',['agentid'=>SHOP_ID,'customerid'=>CUST_ID,'orderid'=>$orderid]);
		$data = $this->J($response);
		
		return view('',['data'=>$data['data']]);
	}
	
	#endregion

}
